package com.zatech.cgnci.project.controller;


import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zatech.cgnci.project.base.enums.ReserveTypeEnum;
import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.dto.CgnciClaimCaseQueryDTO;
import com.zatech.cgnci.project.dto.ProvisionRegisterExtractDTO;
import com.zatech.cgnci.project.dto.ProvisionRegisterQueryDTO;
import com.zatech.cgnci.project.service.CgnciProvisionRegisterService;
import com.zatech.cgnci.project.vo.CgnciClaimCasePageWorkbenchVO;
import com.zatech.cgnci.project.vo.CgnciProvisionRegisterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * <p>
 * 准备金台账管理表 前端控制器
 * </p>
 *
 * @author Generator
 * @since 2025-01-22
 */
@RestController
@RequestMapping("/api/provisionRegister")
@Api(produces = "application/json", tags = "准备金台账报表API")
public class CgnciProvisionRegisterController {

    @Autowired
    private CgnciProvisionRegisterService provisionRegisterService;



    @PostMapping("/page")
    @ApiOperation(value = "分页列表查询")
    public IPage<CgnciProvisionRegisterVO> page(@RequestBody ProvisionRegisterQueryDTO queryDTO) {
        return provisionRegisterService.page(queryDTO);
    }

    @PostMapping("/extract")
    @ApiOperation(value = "准备金台账提取")
    public void extract(@RequestBody ProvisionRegisterExtractDTO extractDTO) {
        provisionRegisterService.extract(extractDTO);
    }

    /**
     * 下载文件
     */
    @GetMapping("/exportAll")
    @ApiOperation(value = "准备金台账文件下载")
    public void exportProjectAll(HttpServletResponse response,Long registerId,String reserveTypeCode){
        try(OutputStream outputStream = response.getOutputStream();
            ExcelWriter writer = provisionRegisterService.export(registerId,reserveTypeCode);) {
            response.setContentType("application/x-download");
            String fileName = URLEncoder.encode(ReserveTypeEnum.getEnumByCode(reserveTypeCode).getName()+".xlsx", "UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            writer.getWorkbook().write(outputStream);
            response.getOutputStream().flush();
        }catch (Exception e){
            throw new GlobalException(ResponseCodeEnum.PARAMS_ILLEGAL, "下载文件失败");
        }
    }
}

